using System;

namespace SkaldRogueLike.Main.Common
{
    public static class StaticRandom
    {
        private static readonly Random Random = new Random();
        private static readonly object SyncLock = new object();
        
        public static int Next(int max)
        {
            if (max <= 0)
                throw new ArgumentException(string.Format("max number should be above zero: {0} <= 0", max));
            if (max == 1)
                return 0;
            lock (SyncLock)
                return Random.Next(max);
        }

        public static int Next(int min, int max)
        {
            if (max <= min)
                throw new ArgumentException(
                    string.Format("max number should be more than min: {1} <= {0}", min, max));
            if (max - min == 1)
                return min;
            lock (SyncLock)
                return Random.Next(min, max);
        }

        public static int NextNotEqualToOld(int min, int max, int oldNumber)
        {
            if (max - min <= 1)
                throw new ArgumentException(
                    string.Format("there is only one possible number between {0} and {1}", min, max));
            if (max - min == 2)
                return min == oldNumber ? min + 1 : min;
            int result;
            do
            {
                lock (SyncLock)
                    result = Random.Next(min, max);
            } while (result == oldNumber);

            return result;
        }
    }
}